package cn.com.kh;

import org.apache.commons.lang.StringUtils;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;
import java.security.SecureRandom;


public class AesUtils {

	/** 
	 * base 64 encode 
	 * @param bytes 待编码的byte[] 
	 * @return 编码后的base 64 code 
	 */  
	public static String base64Encode(byte[] bytes){  
		//编码
		final String encodedText = parseByte2HexStr(bytes);
		return encodedText;
	}  

	/** 
	 * base 64 decode 
	 * @param base64Code 待解码的base 64 code 
	 * @return 解码后的byte[] 
	 * @throws Exception 
	 */ 
	public static byte[] base64Decode(String base64Code) throws Exception{  
		return StringUtils.isEmpty(base64Code) ? null : parseHexStr2Byte(base64Code);
	} 

	/** 
	 * AES加密 
	 * @param content 待加密的内容 
	 * @param encryptKey 加密密钥 
	 * @return 加密后的byte[] 
	 * @throws Exception 
	 */  
	public static byte[] aesEncryptToBytes(String content, String encryptKey) throws Exception {  
		KeyGenerator kgen = KeyGenerator.getInstance("AES");  
		SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG" );
		secureRandom.setSeed(encryptKey.getBytes());
		kgen.init(128, secureRandom);  
		Cipher cipher = Cipher.getInstance("AES");  
		cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(kgen.generateKey().getEncoded(), "AES"));  
		return cipher.doFinal(content.getBytes("utf-8"));  
	}

	/**
	 * 加密
	 * @param content 内容
	 * @return
	 * @throws Exception
	 */
	public static String aesEncrypt(String content,String SECRET_KEY) {
		try {
			return base64Encode(aesEncryptToBytes(content, SECRET_KEY));
		} catch (Exception ex) {
			ex.printStackTrace();
			return "";
		}
	}

	/** 
	 * AES解密 
	 * @param encryptBytes 待解密的byte[] 
	 * @param decryptKey 解密密钥 
	 * @return 解密后的String 
	 * @throws Exception 
	 */  
	public static String aesDecryptByBytes(byte[] encryptBytes, String decryptKey) throws Exception {  
		KeyGenerator kgen = KeyGenerator.getInstance("AES");  
		SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG" );
		secureRandom.setSeed(decryptKey.getBytes());
		kgen.init(128, secureRandom); 
		Cipher cipher = Cipher.getInstance("AES");  
		cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(kgen.generateKey().getEncoded(), "AES"));  
		byte[] decryptBytes = cipher.doFinal(encryptBytes);  
		return new String(decryptBytes);  
	}

	/**
	 * 解密
	 * @param encryptStr 加密内容
	 * @return
	 * @throws Exception
	 */
	public static String aesDecrypt(String encryptStr, String decryptKey) {
		try {
			return StringUtils.isEmpty(encryptStr) ? null : aesDecryptByBytes(base64Decode(encryptStr), decryptKey);
		} catch (Exception ex) {
			ex.printStackTrace();
			return "";
		}
	}

	/**将二进制转换成16进制
	 * @param buf 
	 * @return 
	 */  
	public static String parseByte2HexStr(byte buf[]) {  
		StringBuffer sb = new StringBuffer();  
		for (int i = 0; i < buf.length; i++) {  
			String hex = Integer.toHexString(buf[i] & 0xFF);  
			if (hex.length() == 1) {  
				hex = '0' + hex;  
			}  
			sb.append(hex.toUpperCase());  
		}  
		return sb.toString();  
	}  

	/**将16进制转换为二进制 
	 * @param hexStr 
	 * @return 
	 */  
	public static byte[] parseHexStr2Byte(String hexStr) {  
		if (hexStr.length() < 1)  
			return null;  
		byte[] result = new byte[hexStr.length()/2];  
		for (int i = 0;i< hexStr.length()/2; i++) {  
			int high = Integer.parseInt(hexStr.substring(i*2, i*2+1), 16);  
			int low = Integer.parseInt(hexStr.substring(i*2+1, i*2+2), 16);  
			result[i] = (byte) (high * 16 + low);  
		}  
		return result;  
	} 
	/*
	public static void main(String[] args) throws Exception {  
		try {
			String id = "8B7B14073DCA0376E0533A64640A01A57";
			String encryptCode = aesEncrypt(id, SECRET_KEY);
			System.out.println("加密后：" + encryptCode);
			String decrypt = aesDecrypt(encryptCode, SECRET_KEY);
			System.out.println("解密后：" + decrypt);
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("解密失败");
		}
	}
	*/
}